﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fire : MonoBehaviour {

    public Transform target;
    public int damage = 50;
    public float speed = 15;
    private Animator fire;

    private void Start()
    {
        fire = transform.GetComponent<Animator>();
    }

    public void SetTarget(Transform _target)
    {
        this.target=_target;
    }

    private void Update()
    {
        if(target==null)
        {
            Die();
            return;
        }

        Vector3 direction = target.position - transform.position;
        direction.z = 0f;
        direction = direction.normalized;
        transform.position += direction * Time.deltaTime * speed;
        if(Vector2.Distance(transform.position,target.position)<0.2f)
        {
            fire.SetTrigger("Shoot");
            Invoke("Die", 1);
            target.GetComponent<Car>().TakeDamage(damage);
            
        }

    }

    public void Die()
    {
        GameObject.Destroy(this.gameObject);
    }
}
